import request from '@/utils/request';
import type { UserResponseData, User, AllRoleResponseData, SetRoleData } from './type';

enum API {
  // 获取全部用户信息
  ALLUSER_URL = '/admin/acl/user/',
  //添加用户
  ADDUSER_URL = '/admin/acl/user/save',
  //更新用户信息
  UPDATEUSER_URL = '/admin/acl/user/update',
  //获取所有职位角色信息和当当前用户已有的信息
  ALLROLE_URL = '/admin/acl/user/toAssign/',
  //给已有的用户分配角色
  SETROLE_URL = '/admin/acl/user/doAssignRole',
  //删除用户
  DELETEUSER_URL = '/admin/acl/user/remove/',
  //批量删除用户
  DELETEALLUSER_URL = '/admin/acl/user/batchRemove',
}
/**
 *
 *  获取全部用户信息
 * @param page 页数
 * @param limit 单页数据量
 */
export const reqUserInfo = (page: number, limit: number, username: string) => {
  return request<any, UserResponseData>({
    url: `${API.ALLUSER_URL}${page}/${limit}/?username=${username}`,
    method: 'get',
  });
};

/**
 *
 *  添加或修改全部用户信息
 * @param data 用户信息
 */
export const reqAddOrUpdateUser = (data: User) => {
  if (data.id) {
    return request<any, any>({
      url: API.UPDATEUSER_URL,
      method: 'post',
      data,
    });
  } else {
    return request<any, any>({
      url: API.ADDUSER_URL,
      method: 'post',
      data,
    });
  }
};

/**
 *
 *  获取所有职位角色信息和当当前用户已有的信息
 * @param userId 用户id
 */
export const reqAllRole = (userId: number) => {
  return request<any, AllRoleResponseData>({
    url: API.ALLROLE_URL + userId,
    method: 'get',
  });
};

/**
 *
 *  分配角色
 * @param data SetRoleData
 */
export const reqSetUserRole = (data: SetRoleData) => {
  return request<any, any>({
    url: API.SETROLE_URL,
    method: 'post',
    data,
  });
};

/**
 *
 *  删除用户
 * @param id 用户ID
 */
export const reqDeleteUser = (id: number) => {
  return request<any, any>({
    url: API.DELETEUSER_URL + id,
    method: 'delete',
  });
};

/**
 *
 *  批量删除用户的接口
 * @param idList id列表
 */
export const reqDeleteAllUser = (idList: number[]) => {
  return request<any, any>({
    url: API.DELETEALLUSER_URL,
    method: 'delete',
    data: idList,
  });
};
